//
//  SettingCell.swift
//  BarCode
//
//  Created by xxcao on 2017/3/30.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

import UIKit

typealias ExitBlock = (SettingCell)->(Void)

class SettingCell: UITableViewCell {
    
    @IBOutlet var titleLab :UILabel!
    @IBOutlet var detailLab :UILabel!
    @IBOutlet var arrowImgV :UIImageView!
    
    @IBOutlet weak var pointImg: UIImageView!
    var exitBlock: ExitBlock!
    
    lazy var exitBtn:UIButton = UIButton(type:.custom).then {
//        $0.backgroundColor = UIColor(red: 28.0/255, green: 202.0/255, blue: 147.0/255,alpha: 1)
        $0.backgroundColor = UIColor(red: 255.0/255, green: 53.0/255, blue: 67.0/255,alpha: 1)
        $0.setTitle("退  出", for: .normal)
        $0.setTitleColor(.white, for: .normal)
        $0.addTarget(self, action: #selector(exitApp(sender:)), for:.touchUpInside)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        // Initialization code
        pointImg?.layer.cornerRadius = 4.0
        pointImg?.layer.masksToBounds = true
    }
    
    class func cellWithTableView(tableView: UITableView) -> SettingCell {
        let cellIdentifier = "SettingCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? SettingCell
        if cell == nil {
            cell = Bundle.main.loadNibNamed("SettingCell", owner: self, options: nil)?.first  as? SettingCell
        }
        return cell!
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
    func configureCell(model: CellModel) -> Void {
        if model.indexPath?.section == 3 {
            selectionStyle = .none
            titleLab?.isHidden = true
            detailLab?.isHidden = true
            arrowImgV?.isHidden = true
            pointImg?.isHidden = true
            //退出
            addSubview(exitBtn)
            exitBtn.layer.cornerRadius = 4.0
            exitBtn.layer.masksToBounds = true
            exitBtn.snp.makeConstraints({ make in
                make.left.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-10)
                make.height.equalTo(50)
                make.top.equalToSuperview().offset(20)
            })
            backgroundColor = UIColor(red: 245.0 / 255, green: 245.0 / 255, blue: 245.0 / 255, alpha: 1.0)
        }  else {
            backgroundColor = .white
            titleLab?.text = model.title
            detailLab?.text = model.detail
            
            
            if model.detail == nil || model.detail?.characters.count == 0 {
                arrowImgV?.isHidden = false
                detailLab?.isHidden = true
            } else {
                detailLab?.isHidden = false
                arrowImgV?.isHidden = true
            }
            if model.title == "版本检测" {
                //红点
                if isHasNewVersion {
                    pointImg?.isHidden = false
                } else {
                    pointImg?.isHidden = true
                }
            } else {
                pointImg?.isHidden = true
            }
            
        }
    }
    
    func exitApp(sender : Any) {
        if exitBlock != nil {
            exitBlock!(self)
        }
    }
}
